import axios from "axios";
const axios_instance = axios.create({
	baseURL: '/api',
	timeout: 5000,
});
//axios.defaults.withCredentials = true// 允许当前axios携带cookie
// 添加请求拦截器，在请求头中加token
axios_instance.interceptors.request.use(
	config => {
		if (localStorage.getItem('token')) {
			config.headers.Authorization = localStorage.getItem('token');
		}
		return config;
	},
	error => {
		return Promise.reject(error);
	}
);

axios_instance.interceptors.response.use(
	response => {
		return response;
	},
	error => {
		if (error.response.status == '401') {
			localStorage.removeItem('token');
			window.location.href = '/';
		}
	}
);



//get请求：用于获取数据
export function get(url, params) {
	return axios_instance.get(url, {
		params
	})
}
//post请求：将数据提交
export function post(url, params) {
	return axios_instance.post(url,params)}
//put请求：将数据提交
export function put(url, params) {
	return axios_instance.put(url, {
		params
	})
}
//更新数据
export function patch(url, params) {
	return axios_instance.patch(url, {
		params
	})
}
//delete请求：删除请求
export function Delete(url, params) {
	return axios_instance.Delete(url, {
		params
	})
}
//回显服务器收到的请求，用于测试
export function trace(url, params) {
	return axios_instance.trace(url, {
		params
	})
}
